package com.feihe.repository;

import com.feihe.entity.Reservation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface ReservationRepository extends JpaRepository<Reservation, Long> {
    
    Optional<Reservation> findByUniqid(String uniqid);
    
    List<Reservation> findByUserIdAndIsCheck(Long userId, Integer isCheck);
    
    @Query("SELECT r FROM Reservation r WHERE r.ground = :ground " +
           "AND r.reservationStartTime >= :startTime " +
           "AND r.reservationEndTime <= :endTime")
    List<Reservation> findByGroundAndTimeRange(@Param("ground") Integer ground,
                                              @Param("startTime") LocalDateTime startTime,
                                              @Param("endTime") LocalDateTime endTime);
    
    @Query("SELECT SUM(r.visitorNumber) FROM Reservation r WHERE r.ground = :ground " +
           "AND r.reservationStartTime >= :startTime " +
           "AND r.reservationEndTime <= :endTime")
    Integer sumVisitorNumbersByGroundAndTimeRange(@Param("ground") Integer ground,
                                                 @Param("startTime") LocalDateTime startTime,
                                                 @Param("endTime") LocalDateTime endTime);
    
    /**
     * 查询指定时间范围内的预约记录
     */
    List<Reservation> findByReservationStartTimeBetween(LocalDateTime startTime, LocalDateTime endTime);
} 